﻿using System;
using System.IO;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Swashbuckle.AspNetCore.Swagger;
using testWX.Data;

namespace testWX.Service
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            #region 配置跨域处理
            
            services.AddCors(options =>
            {
                options.AddPolicy("any", builder =>
                {
                    builder.AllowAnyOrigin() //允许任何来源的主机访问
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        .AllowCredentials();//指定处理cookie
                });
            });
            # endregion 配置跨域处理

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            
            #region swagger
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info
                {   Title = "go your uncle",
                    Version = "v1",
                    Description = "Asp.Net Core Webapi with Dapper",
                    TermsOfService = "None",
                    Contact = new Contact
                    {
                        Name = "Jason Zhuang",
                        Email = "aajz@live.cn",
                        Url = "https://github.com/aajz"
                    }
                });
                ////Set the comments path for the swagger json and ui.
                //var basePath = Directory.GetCurrentDirectory();
                //var xmlPath = Path.Combine(basePath, "testWX.Service.xml");
                //c.IncludeXmlComments(xmlPath);

                // Set the comments path for the Swagger JSON and UI.
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });
            services.AddMvcCore().AddApiExplorer();
            #endregion swagger
            var config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json").Build();
            services.AddTransient<IEmployeeProvider>(f => new EmployeeProvider(config["ConnectingString:testWX"]));
            services.AddTransient<IEmployeeProcessor>(f => new EmployeeProcessor(config["ConnectingString:testWX"]));
            
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            #region useSwagger

            // Enable middleware to serve generated Swagger as a JSON endpoint.
            app.UseSwagger();

            // Enable middleware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "TwBusManagement API V1");
                //c.RoutePrefix = string.Empty;
            });

            #endregion
            

            app.UseHttpsRedirection();
            app.UseMvc();

            #region useCORS

            app.UseCors("any");

            #endregion
            
        }
    }
}
